package com.lanou.dllo.shise.com.lanou.dllo.fragment;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.activity.MainActivity;
import com.lanou.dllo.shise.com.lanou.dllo.activity.MessageActivity;
import com.lanou.dllo.shise.com.lanou.dllo.activity.OnMyItemClickListener;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.SearchListAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.Search_countryListAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.entity.SearchBean;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;


/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFragment extends Fragment implements OnMyItemClickListener {
    private RequestQueue requestQueue;
    private SearchListAdapter searchListAdapter;
    private Search_countryListAdapter search_countryListAdapter;
    private RecyclerView fragmentsearchView;
    private RecyclerView search_countryListView;
    private FragmentManager fragmentManager;//定义一个碎片化管理对象


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_search, null);
        return v;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }


    private void initView() {

        fragmentManager = getFragmentManager();
        requestQueue = Volley.newRequestQueue(getActivity());
        fragmentsearchView = (RecyclerView) getActivity().findViewById(R.id.fragmentsearchView);
        searchListAdapter = new SearchListAdapter(getActivity());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),1);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),1);
        fragmentsearchView.setLayoutManager(gridLayoutManager);
        fragmentsearchView.setAdapter(searchListAdapter);
        searchListAdapter.setOnMyItemClickListener(this);
        search_countryListAdapter = new Search_countryListAdapter(getActivity());
        search_countryListView = (RecyclerView) getActivity().findViewById(R.id.search_countryListView);
        search_countryListView.setLayoutManager(layoutManager);
        search_countryListView.setAdapter(search_countryListAdapter);
        RecyclerViewHeader header = (RecyclerViewHeader)getActivity(). findViewById(R.id.header);
        header.attachTo(search_countryListView, true);



    }

    private void initData() {
        String path = "http://api.meishixing.com/user/user/top/city_id=7&session_id=00001227993a5661b25da19c79c2a7c9a6affc";
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SearchBean searchBean = gson.fromJson(response, SearchBean.class);
                if (searchBean != null) {
                    SearchBean.ResultEntity datas = searchBean.getResult();
                    List<SearchBean.ResultEntity.TopUserByCityEntity> city = datas.getTop_user_by_city();
                    List<SearchBean.ResultEntity.TopUserAllEntity> all = datas.getTop_user_all();
                    if (city != null && city.size() > 0 ){
                        searchListAdapter.addData(city);

                    }
                    if (all != null && all.size() > 0 ){
                        search_countryListAdapter.addData(all);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        requestQueue.add(request);

    }





    @Override
    public void onMyItemClick(SearchBean.ResultEntity.TopUserByCityEntity li) {
        Intent intent = new Intent(getActivity(),MessageActivity.class);
        startActivity(intent);


    }

}







